Thread: CTL^D not ending my stdin loop

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    CTL^D not ending my stdin loop

    Can someone explain to me why CTL^D is not ending this loop? My fp has been set to stdin in my main.c Can you also suggest a way that I can do it? Thank You!
    Code:
    #include "main.h"
    
    int fileCheck(FILE *fp)
    {
    
        int line_count = 0;
        char file[BUFF];
        char check[BUFF];
    
        while (fgets(file, BUFF, fp) !=NULL)
        {
            fscanf(fp, "%s", &check);
            if (check == "\n");
            {
                line_count++;
            }
    
        }
        printf("Line count: %d\n", line_count);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What operating system are you using?

    Are you entering this on an empty line?

    Do you realize that fgets() retrieves the end of line character ('\n')?


    Jim

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by jimblumberg View Post
    What operating system are you using?

    Are you entering this on an empty line?

    Do you realize that fgets() retrieves the end of line character ('\n')?


    Jim

    Unix on a Sun Solaris, yes and yes. The problem was fscanf. It was removed. Thank You.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > if (check == "\n");
    Please tell me you're getting warnings for this line.
    It's broken at least twice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by Salem View Post
    > if (check == "\n");
    Please tell me you're getting warnings for this line.
    It's broken at least twice.
    No and I compile with gcc -Wall flag. What do you mean by "broken?"

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Really?
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘fileCheck’:
    foo.c:18:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[100]’ [-Wformat]
    foo.c:19:19: warning: comparison with string literal results in unspecified behavior [-Waddress]
    foo.c:26:1: warning: control reaches end of non-void function [-Wreturn-type]
    $ gcc --version
    gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
    Copyright (C) 2011 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    You can't compare strings with ==, you need to use strcmp()
    The ; at the end of the line renders the if statement useless.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by Salem View Post
    Really?
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘fileCheck’:
    foo.c:18:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[100]’ [-Wformat]
    foo.c:19:19: warning: comparison with string literal results in unspecified behavior [-Waddress]
    foo.c:26:1: warning: control reaches end of non-void function [-Wreturn-type]
    $ gcc --version
    gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
    Copyright (C) 2011 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    You can't compare strings with ==, you need to use strcmp()
    The ; at the end of the line renders the if statement useless.
    Got it, see revised code:
    Code:
    #include "main.h"
    
    int fileCheck(FILE *fp)
    {
    
        int line_count = 0;
        char file[BUFF];
        char check[BUFF];
        char new_line[] = "\n";
        char *line;
        regex_t regex;
    
        /*if (regcomp(&regex, to_find, REG_EXTENDED) != 0)
        {
            fprintf(stderr, "Failed to compile regex '%s'\n", to_find);
            return EXIT_FAILURE;
        }*/
    
        while (fgets(file, BUFF, fp) != NULL)
        {
            if (strcmp(file, new_line) == 0);
            {
                line_count++;
            }
    
            /*if (regexec(&regex, file, 0, NULL, 0) != 0)
            {
                printf("Digit: %s is not alphanumeric on line %d !\n", file, line_count);
                return EXIT_FAILURE;
            }*/
    
        }
        printf("Line count: %d\n", line_count);
    }

  8. #8
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    It seems more likely that you want strchr, which will tell you if '\n' occurs anywhere in the string.

    Code:
    if (strchr(file, '\n')) line_count++;
    Unless you actually want to count empty lines?

    The regular expression library seems like overkill. You can achieve a similar thing with isupper or, if you plan on converting the numeric string, just use strtol/strtod or something.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by Barney McGrew View Post
    It seems more likely that you want strchr, which will tell you if '\n' occurs anywhere in the string.

    Code:
    if (strchr(file, '\n')) line_count++;
    Unless you actually want to count empty lines?

    The regular expression library seems like overkill. You can achieve a similar thing with isupper or, if you plan on converting the numeric string, just use strtol/strtod or something.
    I do want to count empty lines. I must print an error message with the error and which line it occurs on.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > Got it, see revised code:
    Sometimes I wonder why I type.....

    > if (strcmp(file, new_line) == 0);
    Quote Originally Posted by me
    The ; at the end of the line renders the if statement useless.
    Stop messing with regex until you can read a file properly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. waitpid() in loop, not ending correctly
    By Gatsu in forum C Programming
    Replies: 5
    Last Post: 03-21-2013, 02:05 AM
  2. Never ending loop
    By mikeman in forum C++ Programming
    Replies: 3
    Last Post: 02-15-2010, 12:03 PM
  3. Ending after do while loop
    By Chipmunkey in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2007, 01:55 PM
  4. Ending a loop, by pushing SPACE ?
    By The SharK in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2006, 01:53 AM
  5. Problem ending loop
    By carolsue2 in forum C++ Programming
    Replies: 4
    Last Post: 02-16-2006, 12:43 PM